package com.zy.ems.common.jdbc;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * 数据源选择AOP处理。
 * 
 * @author Luffy
 *
 */
@Component
@Aspect
public class MultipleDataSourceAspect implements Ordered {
	/**
	 * 
	 * @param point
	 * @throws Throwable
	 */
	@Before("target(com.zy.ems.common.jdbc.WriteService)")
	public void before(JoinPoint point) throws Throwable {
		// 切换数据源
		MultipleDataSource.setDataSourceKey("masterDataSource");
	}

	/**
	 * 
	 * @param point
	 * @throws Throwable
	 */
	@After("target(com.zy.ems.common.jdbc.WriteService)")
	public void after(JoinPoint point) throws Throwable {
		// 切换数据源
		MultipleDataSource.setDataSourceKey("slaveDataSource");
	}

	/**
	 * Spring中事务与aop的先后顺序问题
	 * 
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.core.Ordered#getOrder()
	 */
	public int getOrder() {
		return 0;
	}

}
